#! /bin/bash
#!/bin/bash
USER="db2inst1"
PWD="123456"
DB="JAMY"
TABLE="TB_USER"
EXPORT_PATH="/opt/output"

# 连接到 DB2 数据库
db2 connect to ${DB} user ${USER} using ${PWD}

# 导出数据为 test.del 文件
db2 "export to ${EXPORT_PATH}/${TABLE}.del of del modified by nochardel coldel0xf8 SELECT * FROM ${TABLE}"

# 读取 test.del 文件内容，并将分隔符修改为 "|"，纵向列采用左对齐方式写入到 test.del 文件中
#awk -v IFS="|" -v OFS="|" '{for(i=1;i<=NF;i++) {gsub(/[[:space:]]/, "fi", $i); gsub(/[^|]/, "& ", $i); printf "%-s\n", $i} print ""}' "${EXPORT_PATH}/${TABLE}.del" > "${TABLE}_1".del

# 断开与 DB2 数据库的连接
db2 connect reset


##数组应用
cd /opt
file=table.txt
##读取文件内容，按照|分割,分割后的字符串，依次存入数组array中，在shell中 array表示数组
IFS='|' read -ra array <"${file}"
#定义一个空数组
my_array=()
for index in "${array[@]}"; do
  ##数组填充
  my_array+=("${index}")
done
echo "${#my_array[*]}"
##遍历数组
for index in "${my_array[@]}"; do
  echo "my_array collection element is ${index}"
done
